#include "MonitoringChangeUser.h"
#include "ui_MonitoringChangeUser.h"

MonitoringChangeUser::MonitoringChangeUser(QWidget *parent,userModel*model) :
    QWidget(parent),
    ui(new Ui::MonitoringChangeUser),
    model(model),
    userSqlite(MonitoringUserSqlite::getInstance())
{
    ui->setupUi(this);
    ui->lineEdit->setText(model->emial);
    connect(ui->pushButton,&QPushButton::clicked,this,[=](){
        LOG_INFO("点击了修改用户信息");
        if(model->emial==ui->lineEdit->text()){
            return ;
        }
        model->emial= ui->lineEdit->text();
        if (SnaillTcpClient::instance()->sendJsonData(MonitoringTool::getUserJson("ChangeEmail",model->username,"",model->emial,""))) {
                   //  qDebug() << "Login data sent successfully";
               } else {
                    ui->label->setText("登录失败，请检查网络");
                   return ;
               }
        connect(SnaillTcpClient::instance(), &SnaillTcpClient::dataReceived, this, &MonitoringChangeUser::handleDataReceived);
//        if(userSqlite.changeEmail(model->username,model->emial)==QUERY_SUCCESS){
//            emit changeClick("修改成功");
//        }else{
//            emit changeClick("修改失败");
//        }

    });
}
void MonitoringChangeUser::handleDataReceived(const QByteArray &data)
{
    qDebug()<<"changeuser==+++=="<<data;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
    QJsonObject jsonObj = jsonDoc.object();
    QString action = jsonObj["action"].toString();
    if(action=="ChangeEmail"){
        QString coded = jsonObj["code"].toString();
        int code = coded.toInt();
        qDebug()<<"code==="<<code;
        if(code==QUERY_SUCCESS){
            qDebug()<<"修改成功";
             ui->label->setText("修改成功");

        }else {
             ui->label->setText("修改失败");
        }

    }


}
MonitoringChangeUser::~MonitoringChangeUser()
{
    delete ui;
}
